#include "USBThread.h"

#include <QDebug>
#include <QFile>
#include "mainwindow.h"

USBThread::USBThread(QObject * parent) :
    QThread(parent)
{
    //dlg = qobject_cast(parent);
    //qDebug(qPrintable(msUSB[0].name));
}

USBThread::~USBThread()
{
    qDebug() << "ThreadFromQThread::~ThreadFromQThread()";
}


void USBThread::run()
{
    int hotplug_sock = init_hotplug_sock();
    while (true) {
        char buf[UEVENT_BUFFER_SIZE * 2] = {0};
        recv(hotplug_sock, &buf, sizeof(buf), 0);
        QString str(buf);
        if(str.startsWith("add@")  && str.indexOf("/block/sd") > 0){
            qDebug() << str;
            MainWindow *ptr = (MainWindow*)parent();
            ptr->setUSBManageRunCount(10);
        }
        if(str.startsWith("remove@") && str.indexOf("/block/sd") > 0){
            qDebug() << str;
            MainWindow *ptr = (MainWindow*)parent();
            ptr->setUSBManageRunCount(10);
        }
    }


    qDebug() << "thread exit!!!" ;
}
